package com.heliang.ui.info;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.example.appxiehui2.R;
import com.heliang.dataaccess.bean.InfoCatBean;

public class InfoCatListAdapter extends BaseAdapter{
	
	private Context context;
	private List<InfoCatBean> infoCatList;
	private LayoutInflater inflater;
	private int mSelectedTab;
	
	public InfoCatListAdapter(Context context,List<InfoCatBean> dataList){
		this.context = context;
		this.infoCatList = dataList;
	}
	
	public int getCount() {
		return Integer.MAX_VALUE;
	}
	
	public Object getItem(int position) {
		return position;
	}

	
	public long getItemId(int position) {
		return position;
	}

	 /**
	  * 设置选中的Tab，并且刷新界面
	  * @param tab
	  */
	public void setSelectedTab(int tab) {
		if (tab != mSelectedTab) {
			mSelectedTab = tab;
			notifyDataSetChanged();
		}
	 }
	/**
	 * 返回选中的Tab
	 * @return
	 */
	public int getSelectedTab() {
		 return mSelectedTab;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		inflater = LayoutInflater.from(context);
		// 自定义的适配器 要用自定义的布局
		// 可以手动创建一个试图也可以用inflater去填充
		TextView rowView = null;
		// convertview 是一个旧的布局
		
//		if (convertview == null ) {
//			convertview = inflater.inflate(com.example.appxiehui2.R.layout.toolbar_row, null);
//			rowView = (TextView) convertview.findViewById(com.example.appxiehui2.R.id.bar_title);
//			convertview.setTag(rowView);
//		} else {
//			rowView = (TextView) convertview.getTag();
//		}
//		
//		rowView.setText(infoCatList.get(position % infoCatList.size()).getInfoCatName());
//		
//		 //对于选中的Tab，给他一个选中的背景
//		 if (position == mSelectedTab){
//			 //rowView.setBackgroundResource(R.drawable.nav_selected);
//		 }else{
//			 //rowView.setBackgroundResource(R.drawable.nav_bg);
//		 }
//		 
////		rowView.setTextColor(Color.WHITE);
////		 rowView.setTextSize(16);//默认单位是sp
////		 System.out.println(infoCatList.get(position).getInfoCatName()+"++++++++++++++++");
////		 rowView.setText(infoCatList.get(position).getInfoCatName() );
////	    rowView.setLayoutParams(new Gallery.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
////		 rowView.setGravity(Gravity.CENTER);
//		 rowView.setPadding(5, 0, 5, 0);
////		 rowView.setTag("aa");
		
		
		
		if (convertView == null ) {
			rowView = new TextView(context);
		} else {
			rowView = (TextView) convertView;
		}
		
		InfoCatBean infoCatBean = infoCatList.get(position % infoCatList.size());
		 rowView.setTextColor(Color.WHITE);
		 rowView.setTextSize(16);//默认单位是sp
		 rowView.setText(infoCatBean.getInfoCatName());
		 rowView.setLayoutParams(new Gallery.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
		 rowView.setGravity(Gravity.CENTER);
		 rowView.setPadding(10, 0, 10, 0);
		 rowView.setTag(infoCatBean);
		 
		 //对于选中的Tab，给他一个选中的背景
		 if (position == mSelectedTab){
			 rowView.setBackgroundResource(R.drawable.nav_selected);
		 }else{
			 rowView.setBackgroundResource(R.drawable.nav_bg);
		 }
			 
			 
		return rowView;
	}
	


}
